#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "book.h"
#include "seqlist.h"

// 添加图书
void AddBook(SeqList* list) {
    Book newBook;
    printf("请输入图书ID：");
    fgets(newBook.id, sizeof(newBook.id), stdin);
    newBook.id[strcspn(newBook.id, "\n")] = '\0';

    if (FindBookIndex(list, newBook.id) != -1) {
        printf("该ID已存在！\n");
        return;
    }

    printf("请输入标题：");
    fgets(newBook.title, sizeof(newBook.title), stdin);
    newBook.title[strcspn(newBook.title, "\n")] = '\0';

    printf("请输入作者：");
    fgets(newBook.author, sizeof(newBook.author), stdin);
    newBook.author[strcspn(newBook.author, "\n")] = '\0';

    printf("请输入出版社：");
    fgets(newBook.publisher, sizeof(newBook.publisher), stdin);
    newBook.publisher[strcspn(newBook.publisher, "\n")] = '\0';

    printf("请输入出版年份：");
    scanf("%d", &newBook.publishYear);
    getchar();

    newBook.status = AVAILABLE;
    newBook.borrower[0] = '\0';

    PushBackBook(list, newBook);
    printf("添加成功！当前总数：%d\n", list->size);
}

// 删除图书
void DeleteBook(SeqList* list) {
    if (list->size == 0) {
        printf("图书馆为空！\n");
        return;
    }
    char id[20];
    printf("请输入要删除的图书ID：");
    fgets(id, sizeof(id), stdin);
    id[strcspn(id, "\n")] = '\0';

    int index = FindBookIndex(list, id);
    if (index == -1) {
        printf("未找到该ID！\n");
        return;
    }

    RemoveBookByIndex(list, index);
    printf("删除成功！\n");
}

// 按ID查询
void SearchBookById(SeqList* list) {
    char id[20];
    printf("请输入要查询的ID：");
    fgets(id, sizeof(id), stdin);
    id[strcspn(id, "\n")] = '\0';

    int index = FindBookIndex(list, id);
    if (index == -1) {
        printf("未找到！\n");
        return;
    }

    Book* b = &list->data[index];
    printf("ID：%s\n标题：%s\n作者：%s\n出版社：%s\n年份：%d\n状态：%s\n",
           b->id, b->title, b->author, b->publisher, b->publishYear,
           b->status == AVAILABLE ? "可借阅" : "已借出");
}

// 按标题/作者搜索
void SearchBookByKeyword(SeqList* list) {
    char keyword[100];
    printf("请输入关键词：");
    fgets(keyword, sizeof(keyword), stdin);
    keyword[strcspn(keyword, "\n")] = '\0';

    int count = 0;
    for (int i = 0; i < list->size; i++) {
        Book* b = &list->data[i];
        if (strstr(b->title, keyword) || strstr(b->author, keyword)) {
            printf("%s\t%s\t%s\t%s\n",
                   b->id, b->title, b->author,
                   b->status == AVAILABLE ? "可借阅" : "已借出");
            count++;
        }
    }
    if (count == 0) printf("未找到相关图书！\n");
}

// 借阅图书
void BorrowBook(SeqList* list) {
    char id[20], name[20];
    printf("请输入借阅图书ID：");
    fgets(id, sizeof(id), stdin);
    id[strcspn(id, "\n")] = '\0';

    int index = FindBookIndex(list, id);
    if (index == -1) {
        printf("未找到该图书！\n");
        return;
    }

    Book* b = &list->data[index];
    if (b->status == BORROWED) {
        printf("该图书已被借出！\n");
        return;
    }

    printf("请输入借阅人姓名：");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';

    b->status = BORROWED;
    strcpy(b->borrower, name);
    b->borrowDate = time(NULL);
    printf("借阅成功！\n");
}

// 归还图书
void ReturnBook(SeqList* list) {
    char id[20];
    printf("请输入归还图书ID：");
    fgets(id, sizeof(id), stdin);
    id[strcspn(id, "\n")] = '\0';

    int index = FindBookIndex(list, id);
    if (index == -1) {
        printf("未找到该图书！\n");
        return;
    }

    Book* b = &list->data[index];
    if (b->status == AVAILABLE) {
        printf("该图书未借出！\n");
        return;
    }

    time_t t = time(NULL);
    double days = difftime(t, b->borrowDate) / (60 * 60 * 24);
    b->status = AVAILABLE;
    b->borrower[0] = '\0';
    printf("归还成功！借阅时长：%.1f天\n", days);
}

// 显示所有图书
void ShowAllBooks(SeqList* list) {
    if (list->size == 0) {
        printf("暂无图书！\n");
        return;
    }
    for (int i = 0; i < list->size; i++) {
        Book* b = &list->data[i];
        printf("%s\t%s\t%s\t%s\t%s\n",
               b->id, b->title, b->author, b->publisher,
               b->status == AVAILABLE ? "可借阅" : "已借出");
    }
}